package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.DayShop;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.StoreInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.SimpleDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class ReachAdapter extends RecyclerView.Adapter<ReachAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private List<DayShop> mList;
    private SimpleDialog selfDialog;

    public ReachAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reach_yet, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.item_name.setText(mList.get(position).getName());
        Glide.with(mContext).load(mList.get(position).getImg_url()).into(holder.item_icon);
        holder.item_tel.setText(mList.get(position).getTel());
        holder.item_user.setText(mList.get(position).getContact());
        holder.item_install.setText(mList.get(position).getAlready_charging_num());
        holder.item_up.setText(mList.get(position).getUse_charging_num());
        if (mList.get(position).getUse_charging_num().equals("0")) {
            holder.item_up_rate.setText("0");
        }else {
            int f = (int) (Integer.parseInt(mList.get(position).getUse_charging_num()) / Float.parseFloat(mList.get(position).getAlready_charging_num()) * 100);

            holder.item_up_rate.setText(f+"%");
        }

        holder.itemView.setTag(position);
//        holder.itemView.setOnClickListener(v -> {
//            onItemClickListenter.itemClickListenter(mList.get(position), position);
//        });

        holder.item_history.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(mList.get(position), position);
        });

        holder.item_tel.setOnClickListener(v -> {
            Dialog(position);

        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView item_icon;
        TextView item_name,item_user,item_tel,item_install,item_up,item_up_rate,item_history;

        public ViewHolder(View itemView) {
            super(itemView);
            item_icon = (ImageView) itemView.findViewById(R.id.item_icon);
            item_name = (TextView) itemView.findViewById(R.id.item_name);
            item_user = (TextView) itemView.findViewById(R.id.item_user);
            item_tel = (TextView) itemView.findViewById(R.id.item_tel);
            item_install = (TextView) itemView.findViewById(R.id.item_install);
            item_up = (TextView) itemView.findViewById(R.id.item_up);
            item_up_rate = (TextView) itemView.findViewById(R.id.item_up_rate);
            item_history = (TextView) itemView.findViewById(R.id.item_history);
        }
    }

    private void Dialog(int posotion) {
        selfDialog = new SimpleDialog(mContext);
        selfDialog.setTitle("提示");
        selfDialog.setMessage(mList.get(posotion).getTel());
        selfDialog.setYesOnclickListener("确定", () -> {
            //用intent启动拨打电话
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mList.get(posotion).getTel()));
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mContext.startActivity(intent);
            selfDialog.dismiss();
        });
        selfDialog.setNoOnclickListener("取消", () -> {
            selfDialog.dismiss();
        });
        selfDialog.show();
    }



    public void addData(List<DayShop> List){
        if (mList != null) {
            mList.clear();
            mList.addAll(List);
            notifyDataSetChanged();
        }
    }

    public void addLastData(List<DayShop> data){
        mList.addAll(data);
        notifyDataSetChanged();
    }


    public interface OnItemClickListenter {
        void itemClickListenter(DayShop dataBean, int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

    public void deleteItem(int position){
        if (mList != null) {
            mList.remove(position);
            notifyDataSetChanged();
        }
    }
}
